<?php
/**
 * Created by PhpStorm.
 * User: kendy
 * Date: 2019/10/14
 * Time: 16:46
 */

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Response; 
use app\admin\model\Settlements as sm;
class Settlements extends Common
{ 
 /* 商家结算列表*/
   public function settlelist()
   { 
        if (request()->isAjax()) {
            $data = input('param.');  
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);  
      			$where['no_settled_order_num'] = ['>',0];
      			$where['is_del'] = 0;
      			$where['is_pass'] = 1; 
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            } 
            $settle = new sm();
			$rdata = $settle->getNoSettleList($where, $this->from,$this->size); 
            ajax_return(config('code.success_show'), '请求成功', $rdata['rd'], $rdata['count']);
        } else {
            return view();
        }
   }

   /**
    * [settledlist 获取商家已结算列表]
    * @Author   kendy
    * @DateTime 2020-01-09T08:55:56+0800
    * @return   [type]                   [description]
    */
   public function settledlist(){ 
    if (request()->isAjax()) {
            $data = input('param.');  
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $where = [];
            $this->getPageAndSize($page);   
            if (isset($data['shop_name']) && $data['shop_name'] != '') {
                $shop_name = array('like', '%' . $data['shop_name'] . '%');
            } 
             if (isset($data['type']) && $data['type'] != '') {
                $where['object_type'] = $data['type'];
            }
              if (isset($data['settlement_time']) && $data['settlement_time'] != ''){
                $created_at=explode(' - ', $data['settlement_time']); 
              if (isset($created_at) && is_array($created_at)){
                foreach($created_at as $k=>$v)
                {
                   $created_at[$k] = strtotime($v);
                }  
              } 
              $where['settlement_time']=['between',$created_at];
           }   
              $shop_name = (!empty(input('param.shop_name')))?input('param.shop_name'):'';
              $hotel =[];
              $tour = [];
              $team = [];
             if(!empty($shop_name)){
                $hotel['hotel_name'] = ['like','%'.$shop_name.'%'];
                $tour['tour_name'] = ['like','%'.$shop_name.'%'];
                $team['team_name'] = ['like','%'.$shop_name.'%'];
             }
            $rdata =  Db::name('settlements') 
            ->field("settlement_no,object_id,settlement_type,settlement_status,object_type,settlement_money,commission_fee,back_money,settlement_time") 
            ->where($where)
            ->order('settlement_id', 'desc')
            ->limit($this->from,$this->size)
            ->select();
            if(count($rdata)>0){
              foreach ($rdata as $key => $value) {
                 $rdata[$key]['shop_name'] = "";
                 $rdata[$key]['settlement_time'] = date("Y-m-d H:i:s",$value['settlement_time']);
                 if($value['settlement_status']==1)$rdata[$key]['settlement_status'] = "已结算";
                 if($value['settlement_type']==0)$rdata[$key]['settlement_type'] = "手工结算";
                 //1时酒店待结算 为2时导游待结 为3时景点待结算 为4时旅行社待结算
                 if($value['object_type']==1){ 
                      $rdata[$key]['shop_name'] = Db::name('hotel')
                      ->where('hotel_id',$value['object_id'])
                      ->value("hotel_name"); 
                 }
                 if($value['object_type']==3){
                  $rdata[$key]['shop_name'] = Db::name('tours')
                      ->where('tour_id',$value['object_id'])
                      ->value("tour_name"); 
                 }
                 if($value['object_type']==4){
                  $rdata[$key]['shop_name'] = Db::name('group_tour')
                      ->alias("gt")
                      ->join("team t",'t.team_id=gt.team_id','left')
                      ->where('id',$value['object_id'])
                      ->value("team_name"); 
                 }
              }

            }
            if($shop_name != ''){  
              foreach($rdata as $key=>$val ){  
                  if (strstr( $val['shop_name'] , $shop_name ) !== false ){  
                    print_r($val);
                    array_push($rdata, $val);
                  } 
              }
            }

            $count =  Db::name('settlements')  
            ->where($where)
            ->order('settlement_id', 'desc')
            ->count();
            ajax_return(config('code.success_show'), '请求成功', $rdata, $count);
        } else {
            return view();
        } 
  
   }
   
   public function orderlist()
   {
	    if (request()->isAjax()) {
            $data = input('param.');  
			$page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);
			if (isset($data['order_sn']) && $data['order_sn'] != ''){
                $where['order_sn'] = array('like','%'.$data['order_sn'].'%');
            }   
      if (isset($data['create_time']) && $data['create_time'] != ''){
                $created_at=explode(' - ', $data['create_time']); 
				if (isset($created_at) && is_array($created_at)){
					foreach($created_at as $k=>$v)
					{
					   $created_at[$k] = strtotime($v);
					}  
				} 
            }  
 			//获取该店铺的待结算的订单 type为1时酒店待结算 为2时导游待结 为3时景点待结算 为4时旅行社待结算
			$where['settlement_id']=0;
			$where['order_status'] = ['IN',[3,4]];
			if($data['type']==1)
			{   $where['ho.is_del']=0;
		        $where['ho.hotel_id']=$data['id'];
		        if(isset($created_at))
			    $where['ho.create_time']=['between',$created_at];
				$orderList = model('HotelOrderModel')->getHotelOrderList($where,$this->from,$this->size);
        if(count($orderList)>0){
          foreach ($orderList as $key => $value) {
               $orderList[$key]['shopName'] =   $value['hotel_name'];
          }
        }
			} 
			elseif($data['type']==3)
			{ 
			    $where['to.is_del']=0;
				$where['to.tour_id']=$data['id'];
			    $orderList = model('TourOrderModel')->getTourOrderList($where,$this->from,$this->size); 
          if(count($orderList)>0){
          foreach ($orderList as $key => $value) {
               $orderList[$key]['shopName'] =   $value['tour_name'];
          }
        }
			} 
			else
			{ 
				$where['go.is_del']=0;
				$where['go.team_id']=$data['id'];
				$orderList = model('GtourOrderModel')->getGtourOrderList($where,$this->from,$this->size);
        if(count($orderList)>0){
          foreach ($orderList as $key => $value) {  
              $orderList[$key]['shopName'] =   $value['gtour_title'];
          }
        }
			}  
              ajax_return(config('code.success_show'), '请求成功', $orderList, 11);
             
        }else{
            //获取商家的类型，和店铺id
            $where['id'] = (int)input('param.id');  
            $where['type'] = (int)input('param.type'); 
			$this->assign('data',$where);
            return $this->fetch();
        }
   }
     public function generateSettle()
     {
       if (Request::instance()->isAjax()){ 
     	  $settle = new sm();
     	  $res = $settle->generateSettle();
        return $res;
     	 
      	}
     }


     public function shoplist(){ 
        if (request()->isAjax()) {
            $data = input('param.');  
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);   
            $where['is_del'] = 0;
            $where['is_pass'] = 1; 
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            } 
            $settle = new sm();
      $rdata = $settle->getshopBankList($where, $this->from,$this->size); 
            ajax_return(config('code.success_show'), '请求成功', $rdata['rd'], $rdata['count']);
        } else {
            return view();
        }
     }

}






